package com.visikard.asynctask;

import java.util.List;

import android.os.Handler;
import android.os.Message;

import com.visikard.application.UIMessage;
import com.visikard.business.AudienceBUS;
import com.visikard.item.ItemAudienceKard;
import com.visikard.response.BaseResponse;
import com.visikard.response.GetAudienceHistoryResponse;

public class GetAudienceHistoryTask extends BaseTask {

	public static final int POS_FK_USER = 0;
	public static final int POS_ID_CAMPAIGN_SPECIAL = 1;
	public static final int POS_PAGE_INDEX = 2;
	public static final int POS_PAGE_SIZE = 3;

	public GetAudienceHistoryTask(Handler handler) {
		super(handler);
	}

	public GetAudienceHistoryTask(Handler handler, boolean isShowProgress) {
		super(handler, isShowProgress);
	}

	@Override
	protected Object doBackgroundWork(Object... params) {

		// Get params
		long fkUser = (Long) params[POS_FK_USER];
		long idCampaignSpecial = (Long) params[POS_ID_CAMPAIGN_SPECIAL];
		int pageIndex = (Integer) params[POS_PAGE_INDEX];
		int pageSize = (Integer) params[POS_PAGE_SIZE];

		// Validation

		// Call service
		AudienceBUS busObj = new AudienceBUS();
		return busObj.getAudienceHistory(fkUser, idCampaignSpecial, pageIndex, pageSize);
	}

	@Override
	protected boolean handleResult(Object result) {
		GetAudienceHistoryResponse response = (GetAudienceHistoryResponse) result;
		if (response != null) {
			switch (response.getErrorCode()) {
				case BaseResponse.SUCCESS:
					List<ItemAudienceKard> listAudienceKards = response.getListItemAudienceKards();
					if (listAudienceKards != null && listAudienceKards.size() > 0) {
						Message successMsg = new Message();
						successMsg.what = UIMessage.MSG_GET_LIST_SUCCESS;
						successMsg.obj = listAudienceKards;
						mUIHandler.sendMessage(successMsg);
					}
					return true;
			}
		}
		return false;
	}

}
